package demo4;

import java.util.*;

public class SQLQueryProcessor {

    // 表示单一关系（即数据库中的一个表）
    static class Relation {
        String name;               // 关系名
        List<String> attributes;   // 属性名列表
        List<Map<String, String>> tuples;  // 数据元组（每个元组是一个Map，属性名 -> 属性值）

        public Relation(String name, List<String> attributes) {
            this.name = name;
            this.attributes = attributes;
            this.tuples = new ArrayList<>();
        }

        // 添加数据元组
        public void addTuple(Map<String, String> tuple) {
            tuples.add(tuple);
        }

        // 显示数据
        public void display() {
            System.out.println("表 " + name + ": " + attributes);
            for (Map<String, String> tuple : tuples) {
                System.out.println(tuple);
            }
        }
    }

    // 实现投影操作
    public static List<Map<String, String>> project(Relation relation, List<String> attributes) {
        List<Map<String, String>> result = new ArrayList<>();
        for (Map<String, String> tuple : relation.tuples) {
            Map<String, String> newTuple = new HashMap<>();
            for (String attribute : attributes) {
                if (tuple.containsKey(attribute)) {
                    newTuple.put(attribute, tuple.get(attribute));
                }
            }
            result.add(newTuple);
        }
        return result;
    }

    // 实现选择操作
    public static List<Map<String, String>> select(Relation relation, String condition) {
        List<Map<String, String>> result = new ArrayList<>();
        String[] parts = condition.split(" ");
        String attribute = parts[0];
        String operator = parts[1];
        String value = parts[2];

        for (Map<String, String> tuple : relation.tuples) {
            String tupleValue = tuple.get(attribute);
            if (tupleValue != null && compareValues(tupleValue, operator, value)) {
                result.add(tuple);
            }
        }
        return result;
    }

    // 比较两个值
    private static boolean compareValues(String tupleValue, String operator, String value) {
        switch (operator) {
            case "=":
                return tupleValue.equals(value);
            case "<":
                return tupleValue.compareTo(value) < 0;
            case ">":
                return tupleValue.compareTo(value) > 0;
            default:
                return false;
        }
    }

    // 实现连接操作
    public static List<Map<String, String>> join(Relation relation1, Relation relation2, String condition) {
        List<Map<String, String>> result = new ArrayList<>();
        String[] parts = condition.split(" ");
        String attribute1 = parts[0];
        String operator = parts[1];
        String attribute2 = parts[2];

        for (Map<String, String> tuple1 : relation1.tuples) {
            for (Map<String, String> tuple2 : relation2.tuples) {
                String value1 = tuple1.get(attribute1);
                String value2 = tuple2.get(attribute2);
                if (value1 != null && value2 != null && compareValues(value1, operator, value2)) {
                    Map<String, String> combinedTuple = new HashMap<>(tuple1);
                    combinedTuple.putAll(tuple2);
                    result.add(combinedTuple);
                }
            }
        }
        return result;
    }

    // 主方法，用于测试
    public static void main(String[] args) {
        // 创建两个表
        List<String> attributes1 = Arrays.asList("id", "name", "age");
        Relation relation1 = new Relation("person", attributes1);
        Map<String, String> tuple1 = new HashMap<>();
        tuple1.put("id", "1");
        tuple1.put("name", "alice");
        tuple1.put("age", "25");
        relation1.addTuple(tuple1);
        Map<String, String> tuple2 = new HashMap<>();
        tuple2.put("id", "2");
        tuple2.put("name", "bob");
        tuple2.put("age", "30");
        relation1.addTuple(tuple2);

        List<String> attributes2 = Arrays.asList("id", "department");
        Relation relation2 = new Relation("department", attributes2);
        Map<String, String> tuple3 = new HashMap<>();
        tuple3.put("id", "1");
        tuple3.put("department", "hr");
        relation2.addTuple(tuple3);
        Map<String, String> tuple4 = new HashMap<>();
        tuple4.put("id", "2");
        tuple4.put("department", "it");
        relation2.addTuple(tuple4);

        // 测试投影
        System.out.println("对 person 表进行投影操作:");
        List<Map<String, String>> projectionResult = project(relation1, Arrays.asList("name", "age"));
        projectionResult.forEach(System.out::println);

        // 测试选择
        System.out.println("对 person 表进行选择操作，条件为 age > 25:");
        List<Map<String, String>> selectionResult = select(relation1, "age > 25");
        selectionResult.forEach(System.out::println);

        // 测试连接
        System.out.println("对 person 和 department 表进行连接操作，条件为 person.id = department.id:");
        List<Map<String, String>> joinResult = join(relation1, relation2, "id = id");
        joinResult.forEach(System.out::println);
    }
}
